/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/

package com.genia.toolbox.web.gwt.basics.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.genia.toolbox.web.gwt.basics.client.GwtHelper;
import com.genia.toolbox.web.gwt.basics.client.bean.GwtLocale;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.SourcesTabEvents;
import com.google.gwt.user.client.ui.TabListener;

/**
 * a {@link TabListener} that allows all locale text to be synchronized.
 */
public class LocaleTabListener
    implements TabListener, Command
{

  /**
   * the list of locales handled by the application.
   */
  private static GwtLocale[] locales = null;

  /**
   * the current selected locale.
   */
  private static int selectedLocale = 0;

  /**
   * the {@link List} of {@link LazyTabPanel} that are the locale text widgets.
   */
  static final List<LazyTabPanel> LAZY_TAB_PANELS_TO_CHANGE = new ArrayList<LazyTabPanel>();

  /**
   * the shared {@link TabListener}.
   */
  static final TabListener LOCALE_TAB_LISTENER = new LocaleTabListener();



  /**
   * the synchronization method.
   * 
   * @see com.google.gwt.user.client.Command#execute()
   */
  public void execute()
  {
    for (LazyTabPanel lazyTabPanel : LAZY_TAB_PANELS_TO_CHANGE) {
      lazyTabPanel.selectTab(selectedLocale);
    }
  }



  /**
   * /** Fired just before a tab is selected.
   * 
   * @param sender
   *          the {@link com.google.gwt.user.client.ui.TabBar} or
   *          {@link com.google.gwt.user.client.ui.TabPanel} whose tab was
   *          selected.
   * @param tabIndex
   *          the index of the tab about to be selected
   * @return <code>false</code> to disallow the selection. If any listener
   *         returns false, then the selection will be disallowed.
   */
  public boolean onBeforeTabSelected(SourcesTabEvents sender, int tabIndex)
  {
    return true;
  }



  /**
   * Fired when a tab is selected.
   * 
   * @param sender
   *          the {@link com.google.gwt.user.client.ui.TabBar} or
   *          {@link com.google.gwt.user.client.ui.TabPanel} whose tab was
   *          selected
   * @param tabIndex
   *          the index of the tab that was selected
   */
  public void onTabSelected(SourcesTabEvents sender, int tabIndex)
  {
    if (tabIndex != selectedLocale) {
      selectedLocale = tabIndex;
      DeferredCommand.addCommand(this);
    }
  }



  /**
   * the async method associated to getRegisteredLocale().
   * 
   * @param callback
   *          the callback
   */
  static void getRegisteredLocale(final AsyncCallback<GwtLocale[]> callback)
  {
    if (locales != null) {
      callback.onSuccess(locales);
      return;
    }
    GwtHelper.getI18nService().getRegisteredLocale(new AsyncCallback<GwtLocale[]>()
    {

      public void onFailure(Throwable cause)
      {
        callback.onFailure(cause);
      }



      public void onSuccess(GwtLocale[] result)
      {
        locales = result;
        callback.onSuccess(locales);
      }

    });

  }



  /**
   * returns the currently selected locale.
   * 
   * @return the currently selected locale
   */
  public static int getSelectedLocale()
  {
    return selectedLocale;
  }

}
